package com.usoft;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;

/**
 * Created by liyanxin on 2015/1/8.
 */
public class HelloWorld {

    /**
     * 在这里实现这样一个功能，A actor给 B actor 发送消息，接收后返回消息说已收到
     */
    public static class A extends UntypedActor {
        @Override
        public void preStart() throws Exception {

            // 使用当前actor的context 创建了一个子actor，B actor就是A actor的子actor
            // using an actor’s context will create a child actor
            final ActorRef child =
                    getContext().actorOf(Props.create(B.class), "myChild");
            child.tell("good moring", this.getSelf());
        }

        @Override
        public void onReceive(Object message) throws Exception {
            if (message instanceof String) {
                System.out.println("接收到B Actor的消息：" + message);
                getContext().stop(getSelf());
            }
        }
    }

    public static class B extends UntypedActor {

        @Override
        public void onReceive(Object message) throws Exception {
            if (message instanceof String) {
                System.out.println("接收到A Actor的消息：" + message);
                this.getSender().tell("thank you!", this.getSelf());
            }
        }
    }


    public static void main(String args[]) {

        ActorSystem system = ActorSystem.create("myActorSystem");

        // Actors are created by passing a Props instance into the actorOf factory method which is available on
        // ActorSystem and ActorContext.
        // 通过ActorSystem 和 ActorContext的工场方法actorOf创建actor
        // 工场方法需要接收一个Props instance
        system.actorOf(Props.create(A.class), "helloWorld");
    }
}
